import { saveOperateLogAPI } from '../api/operate_log'
import { getUserInfoAPI } from '../api/userinfo'




export const tracking = async (type, type2, operation_object, operation_level) => {

  const name = JSON.parse(localStorage.getItem('userinfo')).name ? JSON.parse(localStorage.getItem('userinfo')).name : `用户${JSON.parse(localStorage.getItem('userinfo')).account}`
  let object = ''
  let operation_content = ''
  let identity = ''

  // 用户管理
  if (type == "用户管理") {
    // 传的 id
    if (typeof operation_object === 'number') {
      const res = await getUserInfoAPI(operation_object)
      object = res.data.data.name
      identity = res.data.data.identity
      operation_content = `${name}对${identity}'${object}'进行了${type2}操作`
    } else {
      // 传的 name
      object = operation_object
      operation_content = `${name}${type2}: ${object}`
    }
    await saveOperateLogAPI({ operation_person: name, operation_content, operation_level })
  }
  // 产品管理
  if (type == "产品管理") {
    object = operation_object
    if (type2 == "入库" || type2 == "申请出库") {
      operation_content = `${name}${type2}'${object.name}'${object.number}${object.unit}`
    }
    if (type2 == "编辑") {
      operation_content = `${name}对产品：'${object.name}'进行了${type2}信息操作`
    }
    if (type2 == "删除") {
      operation_content = `${name}删除了产品：'${object.name}'`
    }
    if (type2 == "审核") {
      operation_content = `${name}${object.result}了'${object.name}'的出库`
    }
    if (type2 == "再次申请出库") {
      operation_content = `${name}${type2}'${object.name}'${object.number}个`
    }
    await saveOperateLogAPI({ operation_person: name, operation_content, operation_level })
  }
  // 消息管理
  if (type == "消息管理") {
    if (type2 == "发布公司公告" || type2 == "发布系统消息" || type2 == "编辑公司公告" || type2 == "编辑系统消息" || type2 == "删除公司公告" || type2 == "删除系统消息") {
      type2 = type2.slice(0, 2) + "了" + type2.slice(2)
    } else if (type2 == "永久删除消息") {
      type2 = type2.slice(0, 3) + "了" + type2.slice(3)
    }
    object = operation_object
    operation_content = `${name}${type2}: ${object.title}`
    await saveOperateLogAPI({ operation_person: name, operation_content, operation_level })
  }
  // 合同管理
  if (type == "合同管理") {
    if (type2 == "上传文件" || type2 == "下载文件" || type2 == "删除文件") {
      type2 = type2.slice(0, 2) + "了" + type2.slice(2)
    }
    object = operation_object
    operation_content = `${name}${type2}: ${object.name}`
    await saveOperateLogAPI({ operation_person: name, operation_content, operation_level })
  }
  // 系统设置
  if (type == "系统设置") {
    if (type2 == "修改头像" || type2 == "修改密码" || type2 == "修改姓名" || type2 == "修改性别" || type2 == "修改公司名称" || type2 == "修改首页轮播图" || type2 == "修改公司介绍") {
      type2 = type2.slice(0, 2) + "了" + type2.slice(2)
      operation_content = `${name}${type2}`
      await saveOperateLogAPI({ operation_person: name, operation_content, operation_level })
    }
  }
}